package com.coursemanage.account;

import com.coursemanage.common.model.Manager;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;

public class AccountController extends Controller {


	public void logout(){
		removeCookie("UserName");
		removeCookie("UserId");
		redirect("/admin/account/login.jsp");
	}
	public void login() {
		String UserId=getCookie("UserId");
		if (UserId!=null&&UserId!="") {
			redirect("/admin/index.jsp");
			return;	
		}	
		render("login.jsp");	
	}
	@Before(LoginValidator.class)
	public void checklogin() {
		String password=HashKit.md5(getPara("password")).toUpperCase();
		String name=getPara("name");
		String sql = "select *  from manager where user_name=? and password=?";
		Manager users=Manager.dao.findFirst(sql,name,password);
		if(users!=null){
			setCookie("UserName", users.getStr("user_name"), 3600);
			setCookie("UserId", users.getId().toString(), 3600);
			redirect("/admin/index.jsp");
		}else{
			setAttr("msg", "账号或者密码错误");
			setAttr("name",name);
			render("login.jsp");
		}
		
	}
}
